package com.quantgroup.data.producer;

import org.apache.kafka.clients.producer.*;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;
import java.util.concurrent.ExecutionException;

/**
 * 客户端生产者
 *
 * @author yangjinhua
 */
public class CustomProducer {
    public static void main(String[] args) {

        try {
//        noCallback();
//        callback();
            syncSend();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 生产不带回调函数
     */
    private static void noCallback() {
        Properties props = new Properties();
        //kafka 集群， broker list
        //先走单点
        props.put("bootstrap.servers", "192.168.5.152:9092");
        props.put("acks", "all");
        //重试次数
        props.put("retries", 1);
        //批次大小
        props.put("batch.size", 16384);
        //等待时间
        props.put("linger.ms", 1);
        //RecordAccumulator 缓冲区大小
        props.put("buffer.memory", 33554432);
        props.put("key.serializer", StringSerializer.class);
        props.put("value.serializer", StringSerializer.class);
//        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
//        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        Producer<String, String> producer = new KafkaProducer<>(props);
        for (int i = 0; i < 100; i++) {
            System.out.println("当前是i：" + i);
            producer.send(new ProducerRecord<>("first", Integer.toString(i), Integer.toString(i)));
        }
        producer.close();

//        //创建生产者实例
//        KafkaProducer<String,String> producer = new KafkaProducer<>(props);
//        ProducerRecord record = new ProducerRecord<>("first", "userName", "lc");
//        //发送记录
//        producer.send(record);
//        producer.close();

    }


    /**
     * 生产带回调函数
     */
    private static void callback() {
        Properties props = new Properties();
        //kafka 集群， broker list
        //先走单点
        props.put("bootstrap.servers", "192.168.5.152:9092");
        props.put("acks", "all");
        //重试次数
        props.put("retries", 1);
        //批次大小
        props.put("batch.size", 16384);
        //等待时间
        props.put("linger.ms", 1);
        //RecordAccumulator 缓冲区大小
        props.put("buffer.memory", 33554432);
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        Producer<String, String> producer = new KafkaProducer<>(props);
        for (int i = 0; i < 100; i++) {
            producer.send(new ProducerRecord<>("first", Integer.toString(i), Integer.toString(i)), new Callback() {
                @Override
                public void onCompletion(RecordMetadata recordMetadata, Exception e) {
                    //回调函数， 该方法会在 Producer 收到 ack 时调用，为异步调用
                    if (e == null) {
                        System.out.println("success -->" + recordMetadata.offset());
                    } else {
                        e.printStackTrace();
                    }
                }
            });
        }
        producer.close();
    }


    /**
     * 生产同步发送API
     */
    private static void syncSend() throws ExecutionException, InterruptedException {
        Properties props = new Properties();
        //kafka 集群， broker list
        props.put("bootstrap.servers", "192.168.5.152:9092");
        props.put("acks", "all");
        // 重试次数
        props.put("retries", 1);
        // 批次大小
        props.put("batch.size", 16384);
        // 等待时间
        props.put("linger.ms", 1);
        //RecordAccumulator 缓冲区大小
        props.put("buffer.memory", 33554432);
        props.put("key.serializer", StringSerializer.class);
        props.put("value.serializer", StringSerializer.class);
        Producer<String, String> producer = new KafkaProducer<>(props);
        producer.send(new ProducerRecord<>("first1", Integer.toString(1), Integer.toString(1))).get();
//        for (int i = 0; i < 100; i++) {
//            producer.send(new ProducerRecord<>("first", Integer.toString(i), Integer.toString(i))).get();
//        }
        producer.close();
    }
}